



//
//  WZUserModel.m
//  weibo
//
//  Created by mac on 16/2/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "WZUserViewModel.h"
#import "AFNetworking.h"

@implementation WZUserViewModel

static id _instance;
//重新写到单例模式
+(instancetype)shareInstance{
    static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [[super alloc]init];
        });
    return _instance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
//必须要实现NScoding协议，但是实现方法后可以删掉协议
-(id)copyWithZone:(NSZone *)zone{
    return _instance;
}


-(BOOL)isLogin{
     self.userModel = [WZUserInfoModel readObjectWithFileName:@"aa.text"];
    NSLog(@"%@",self.userModel.access_token);
   
    if (self.userModel.access_token == nil) {
        return NO;
    }else{
        return YES;
    }
}



-(void)getAccessTokenWithCode:(NSString *)code andSuccess:(finished)success andFaile:(finished)fail{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain",nil];
    
    NSDictionary *parameter =
    @{@"client_id": @"517511500",
      @"client_secret":@"d01322253f742e80c619cb306a7d753d",
      @"grant_type":@"authorization_code",
      @"code":code,
      @"redirect_uri": @"http://www.baidu.com"};
   [ manger POST:@"https://api.weibo.com/oauth2/access_token" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
       
       NSDictionary *dict = (NSDictionary*)responseObject;
       

           WZUserInfoModel *userModel = [WZUserInfoModel userInfoModelWithDic:dict];
       
       
       [self getUserInfoWithUserModel:userModel andSuccess:success andFaile:fail];
       
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
        fail();
    }];
    
    
}
-(void)getUserInfoWithUserModel:(WZUserInfoModel *)userModel andSuccess:(finished)success andFaile:(finished)fail{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
//    access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
//    uid	false	int64	需要查询的用户ID。
    
    NSDictionary *parameter = @{@"access_token":userModel.access_token,
                                @"uid":userModel.uid};
    [manger GET:@"https://api.weibo.com/2/users/show.json" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        success();
        [userModel saveUserPersonToDocumentWithFileName:@"aa.text"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail();
    }];
    
    
}
@end
